package com.tarry.shop.user.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tarry.shop.common.ResultEntity;
import com.tarry.shop.exception.BusinessException;
import com.tarry.shop.user.entity.User;
import com.tarry.shop.user.service.IUserService;
import com.tarry.shop.validation.Add;
import com.tarry.shop.validation.Query;
import com.tarry.shop.validation.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author TARRY
 * @since 2021-11-21
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户信息",tags = "用户信息")
@Validated
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/add")
    @ApiOperation(value = "添加用户")
    public ResultEntity<User> addUser(@Validated(value = Add.class) User user){
        userService.save(user);
        return ResultEntity.success(user);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新用户信息")
    public ResultEntity<User> updateUser(@Validated(value = Update.class) User user){
        userService.updateById(user);
        return ResultEntity.success(user);
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除用户")
    public ResultEntity<Boolean> delete(@ApiParam(required = true,value = "用户Id") @NotNull(message = "ID不能为空") Integer userId){
       return ResultEntity.success(userService.removeById(userId));
    }

    @GetMapping("/queryList")
    @ApiOperation(value = "分页查询用户信息")
    public ResultEntity<IPage<User>> queryPage(Page<User> page,@Validated(value = Query.class) User user){
        Wrapper<User> queryWrapper = new QueryWrapper<User>()
                .likeRight(StringUtils.isNotBlank(user.getUserName()),"user_name",user.getUserName())
                .likeRight(StringUtils.isNotBlank(user.getPhone()),"phone",user.getPhone())
                .ge(Objects.nonNull(user.getSex()),"sex",user.getSex())
                .likeRight(StringUtils.isNotBlank(user.getEmail()),"email",user.getEmail());
        IPage<User> userPage = userService.page(page,queryWrapper);
        return ResultEntity.success(userPage);
    }

    @GetMapping("/getUserById")
    @ApiOperation(value = "根据用户ID查询用户信息")
    public ResultEntity<User> getUserById(@ApiParam(required = true,value = "用户Id")
                                              @NotNull(message = "ID不能为空") Integer userId){
        return ResultEntity.success(userService.getById(userId));
    }


}
